import telnetlib, time,re


class telClass():
	ip = '127.0.0.1'
	port = 2605
	pwd = 'zebra'
	timeout = 50


def telnet_no_network():
	tn = telnetlib.Telnet(telClass.ip, port=telClass.port, timeout=telClass.timeout)  # 23
	tn.read_until('Password:'.encode())
	tn.write('zebra\n'.encode())
	print('pwd end')
	time.sleep(1)
	tn.read_until('bgpd>'.encode())
	print('login success')
	tn.write('ena\n'.encode())
	time.sleep(1)
	tn.write('show run\n'.encode())
	print('write end')
	time.sleep(1)
	result = tn.read_very_eager()
	tn.write('q\n'.encode())
	tn.write('q\n'.encode())
	tn.close()
	return result.decode()

def getConfigData(data):
	ReData = {'reouter':None,'IPs':[]}
	lines = list(map(lambda x:x.replace('\r',''),data.split('\n')))
	for line in lines:
		r1 = re.search('bgp router-id', line)
		if r1:
			ReData['reouter']=line.replace('bgp router-id', '').replace(' ', '')
		r2 = re.search('network', line)
		if r2:
			ReData['IPs'].append(line.replace('network', '').replace(' ', ''))
	return ReData
if __name__ == '__main__':
	result = (telnet_no_network())
	print(getConfigData(result))
